define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    

    $.widget('rk.exportApprovalDialogCtrl', $.rk.oaDialogCtrl, {
        create: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
        },
        _init: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._initApprovalExport();
            me.getBtn('ok').on('click', $.proxy(me.okClicked, me));
            me.getBtn('cancel').on('click', $.proxy(me.cancelClicked, me));
        },
        _initApprovalExport: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            rk.post('/json/oa_approval/init-search-form.action', {}).done(function (json) {
                if (json.status == 0) {
                    json.data.viewType = opt.viewType;
                    var html = rk.templateText(require('oa/tmpl/approval/tpl_approval_dialog_export.tpl'), json.data);
                    elem.html(html);
                    $('#flowCategoryId').selectmenu({
                        appendTo: 'body'
                    });
                    $('#exportStartDate').datepicker({changeMonth: true, changeYear: true});
                    $('#exportEndDate').datepicker({changeMonth: true, changeYear: true});
                    $('#departSelectForExport').multitreeselector({source: '/json/crm_dimension/dim-tree.action?dimType=1&belongId=13'});
                    $('#subUser').multipeopleselector({
                        position: {
                            my: 'left top+2'
                        }
                    });
                } else {
                    $.msg(rk.i18n('LOAD_ERROR'));
                }
                rk.repositionDialog()
            });
        },
        okClicked: function () {
            var me = this;
            var elem = me.element;
            var start = $.trim($('#exportStartDate').val());
            var end = $.trim($('#exportEndDate').val());
            if (start == '' || end == '') {
                $.msg(rk.i18n('VALIDATE_DATE_REQUIRE_ERROR'));
                return;
            }
            if (start > end) {
                $.msg(rk.i18n('VALIDATE_DATE_COMPARE_ERROR'));
                return;
            }
            var departArray = [];
            $.each($('#departSelectForExport').multitreeselector('val'), function (i, o) {
                departArray.push(o.value);
            });
            $("#departIdsForExport").val(departArray.length < 1 ? '' : departArray.join(","));
            var userArray = [];
            $.each($('#subUser').multipeopleselector("val"), function (i, o) {
                userArray.push(o.value);
            });
            $("#userIdsForExport").val(userArray.length < 1 ? '' : userArray.join(","));
            $("#exportForm").attr("action", "/json/oa_approval/export.action");
            $('#exportForm').submit();
            me.closeMe();
        },
        cancelClicked: function () {
            var me = this;
            var elem = me.element;
            me.closeMe();
        }
    });

    return $;
});